<?php
require_once("Service.php");
require_once("Database.php");

class Forum extends Service {
	
	var $conn;
	
	/** The constructor of this object. */
	function __construct() {
		
		parent::__construct();
		$this->conn = new Database();
	}
	
	/**
	 * Returns a list of forum posts.
	 * @param $sessionID The session id of the user calling this function.
	 * @return An array of forum posts.
	 */
	function getPostList($sessionID) {
		
		$this->validateSession($sessionID);
		
		$result = $this->conn->execute("
			SELECT
				a.postID,
				b.firstName,
				b.lastName,
				a.header,
				a.date
			FROM Posts a
			LEFT OUTER JOIN Users b
				ON a.userID = b.userID
			WHERE a.postID > 1
		");
		
		$posts = array();
		while($row = mysql_fetch_assoc($result)) {
			$posts[] = $row;
		}
		return $posts;
	}
	
	/**
	 * Returns the details and comments of a forum post.
	 * @param $sessionID The session id of the user calling this function.
	 * @param $postID The id of the post to retrieve.
	 * @return A object containing the post details.
	 */
	function getPostDetails($sessionID, $postID) {
		
		$this->validateSession($sessionID);
		
		// get post details
		$result = $this->conn->execute("
			SELECT
				a.postID,
				b.firstName,
				b.lastName,
				a.header,
				a.body,
				a.date
			FROM Posts a
			LEFT OUTER JOIN Users b
				ON a.userID = b.userID
			WHERE a.postID = " . $postID . "
		");
		
		$return = mysql_fetch_assoc($result);
		
		// get post comments
		$result = $this->conn->execute("
			SELECT
				b.firstName,
				b.lastName,
				a.header,
				a.body,
				a.date
			FROM Comments a
			LEFT OUTER JOIN Users b
				ON a.userID = b.userID
			WHERE a.postID = " . $postID . "
		");
		
		$comments = array();
		while($row = mysql_fetch_assoc($result)) {
			$comments[] = $row;
		}
		$return["comments"] = $comments;
		
		return $return;
	}
	
	/**
	 * Saves a new forum post.
	 * @param $sessionID The session id of the user calling this function.
	 * @param $userID The id of the user doing the update.
	 * @param $header The header of the post.
	 * @param $body The body of the post.
	 * @return The id of the saved post.
	 */
	function savePost($sessionID, $userID, $header, $body) {
		
		$this->validateSession($sessionID);
		
		$rowID = $this->conn->insert("
			INSERT INTO Posts (userID, header, body, date)
			VALUES (
				" . $userID . ",
				'" . $header . "',
				'" . $body . "',
				'" . date("Y-m-d H:i:s") . "'
			);
		");
		
		return $rowID;
	}
	
	/**
	 * Adds a new comment on a post.
	 * @param $sessionID The session id of the user calling this function.
	 * @param $postID The id of the post to comment on.
	 * @param $userID The id of the user doing the update.
	 * @param $header The header of the post.
	 * @param $body The body of the post.
	 */
	function saveComment($sessionID, $postID, $userID, $header, $body) {
		
		$this->validateSession($sessionID);
		
		$result = $this->conn->execute("
			INSERT INTO Comments (postID, userID, header, body, date)
			VALUES (
				" . $postID . ",
				" . $userID . ",
				'" . $header . "',
				'" . $body . "',
				'" . date("Y-m-d H:i:s") . "'
			);
		");
	}
}
?>